package com.xiaoxin.smartbi.mqtest.mq3;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;

public class ReceiveLogs {
  private static final String EXCHANGE_NAME = "logs";

  public static void main(String[] argv) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();

    //队列1
    Channel channel1 = connection.createChannel();
    channel1.queueDeclare("小王", true, false, false, null);
    channel1.exchangeDeclare(EXCHANGE_NAME, "fanout");
    String queueName1 = channel1.queueDeclare().getQueue();
    channel1.queueBind(queueName1, EXCHANGE_NAME, "");

    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    DeliverCallback deliverCallback1 = (consumerTag, delivery) -> {
        String message = new String(delivery.getBody(), "UTF-8");
        System.out.println(" [x] Received '" + message + "'");
    };
    channel1.basicConsume(queueName1, false, deliverCallback1, consumerTag -> { });

    //队列2
    Channel channel2 = connection.createChannel();
    channel2.queueDeclare("小李", true, false, false, null);
    channel2.exchangeDeclare(EXCHANGE_NAME, "fanout");
    String queueName2 = channel2.queueDeclare().getQueue();
    channel2.queueBind(queueName2, EXCHANGE_NAME, "");

    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    DeliverCallback deliverCallback2 = (consumerTag, delivery) -> {
      String message = new String(delivery.getBody(), "UTF-8");
      System.out.println(" [x] Received '" + message + "'");
    };
    channel2.basicConsume(queueName2, false, deliverCallback2, consumerTag -> { });
  }
}